package com.hecore.lagou.mr.component;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 22:19
 * @Description:  Combiner的意义就是对每⼀个maptask的输出进⾏局部汇总
 *
 * ⾃定义⼀个Combiner继承Reducer，重写Reduce⽅法
 * 在驱动(Driver)设置使⽤Combiner（默认是不适⽤Combiner组件）
 */

public class WcCombine extends Reducer<Text, IntWritable,Text,IntWritable> {
    IntWritable total = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context
            context) throws IOException, InterruptedException {
        //2 遍历key对应的values，然后累加结果
        int sum = 0;
        for (IntWritable value : values) {
            int i = value.get();
            sum += 1;
        }
        // 3 直接输出当前key对应的sum值，结果就是单词出现的总次数
        total.set(sum);
        context.write(key, total);
    }
}
